package com.tedu.weibo.controller;

import com.tedu.weibo.comment.result.JsonResult;
import com.tedu.weibo.comment.result.StatusCode;
import com.tedu.weibo.mapper.WeiboMapper;
import com.tedu.weibo.pojo.dto.WeiboDTO;
import com.tedu.weibo.pojo.entity.User;
import com.tedu.weibo.pojo.entity.Weibo;
import com.tedu.weibo.pojo.vo.WeiboDetailVO;
import com.tedu.weibo.pojo.vo.WeiboIndexVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags = "微博模块")
@RestController
@RequestMapping("/v1/weibo")
@Slf4j
@Validated
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    @ApiOperation("发表微博")
    @PostMapping("/insert")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session){
        log.info("WeiboController.insert(),发表微博");
        log.trace("开始执行insert方法");
        log.debug("weiboDTO:"+weiboDTO);
        User user=(User)session.getAttribute("user");
        log.trace("通过session获取用户登录信息");
        log.debug("user:"+user);
        if (user==null){ //未登录
            log.trace("user 为null，未登录");
            return new JsonResult(StatusCode.NOT_LOGIN);
        }else {//已登录
            log.trace("user 不为null，处理发表微博");
            Weibo weibo=new Weibo();
            BeanUtils.copyProperties(weiboDTO,weibo);
            weibo.setCreated(new Date());
            weibo.setUserId(user.getId());
            log.debug("weibo"+weibo);
            weiboMapper.insert(weibo);
            log.trace("插入微博完毕");
            //return new JsonResult(StatusCode.OPERATION_SUCCESS);
            return  JsonResult.sucess();
        }

    }

    @ApiOperation("首页微博列表")
@GetMapping("/selectIndex")
    public JsonResult selectIndex(){
        List<WeiboIndexVO> list=weiboMapper.selectIndex();
       // return new JsonResult(StatusCode.OPERATION_SUCCESS,list);
        return JsonResult.sucess(list);

    }

    @ApiOperation("微博详情")
    @ApiImplicitParam(name="id",value="微博的ID" ,required=true,dataType="int",example="1")
    @GetMapping("/selectById")
    public JsonResult WeiboDetailtById(@Range(min =1,max =999,message = "微博id必须在1-999之间") int id){

        WeiboDetailVO vo=weiboMapper.selectById(id);
       // return new JsonResult(StatusCode.OPERATION_SUCCESS,vo);
        return JsonResult.sucess(vo);

    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "微博id",required = true,dataType = "int",example = "1"),
            @ApiImplicitParam(name="name",value = "用户名称",required = true,dataType = "String",example = "李四")
    })
    @ApiOperation("测试注释")
    @GetMapping("/selectByIdAndName")
    public WeiboDetailVO selectByIdAndName(int id,String name){
        return weiboMapper.selectById(id);
    }

}
